<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>async函数的基本用法</title>
</head>
<body>
    <h1>async函数的基本用法</h1>

    <script>
        // 基本用法
        async function getStockPrice(name) {
            const symbol = await getStockSymbol(name);
            const stockPrice = await getStockPrice(symbol);
            return stockPrice;
        }

        // 函数声明
        async function foo() {}

        // 函数表达式
        const bar = async function() {};

        // 对象的方法
        let obj = { 
            async foo() {} 
        };

        // Class 的方法
        class Storage {
            constructor() {
                this.cachePromise = caches.open('avatars');
            }

            async getAvatar(name) {
                const cache = await this.cachePromise;
                return cache.match(`/avatars/${name}.jpg`);
            }
        }

        // 箭头函数
        const foo2 = async () => {};

        // 实际应用示例
        async function loadData() {
            try {
                const response = await fetch('https://api.example.com/data');
                const data = await response.json();
                return data;
            } catch (err) {
                console.log('Error:', err);
                throw err;
            }
        }

        // 并发执行
        async function parallel() {
            const [result1, result2] = await Promise.all([
                fetch('url1'),
                fetch('url2')
            ]);
            return [await result1.json(), await result2.json()];
        }
    </script>
</body>
</html> 